package com.niit.bc.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.RowSet;

import com.niit.bc.dao.ListDao;
import com.sun.rowset.CachedRowSetImpl;

public class ListDaoImpl extends BaseDao implements ListDao {
    public RowSet findAllList() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        CachedRowSetImpl crsi = null;
        String sql = "select qa_topics.typeId ,qa_types.typeName,topicId, topicName " +
                "from qa_topics left join qa_types  on qa_types.typeId = qa_topics.typeId";

        conn = this.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            crsi = new CachedRowSetImpl();
            crsi.populate(rs);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeSource(rs, null, ps, conn);
        }

        return crsi;
    }


    public RowSet findList(int typeId) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        CachedRowSetImpl crsi = null;
        String sql =
                "select * from bc_types left join bc_childTypes on bc_types.typeId = bc_childTypes.typeId where bc_types.typeId =?";

        conn = this.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, typeId);
            rs = ps.executeQuery();
            crsi = new CachedRowSetImpl();
            crsi.populate(rs);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            this.closeSource(rs, null, ps, conn);
        }

        return crsi;
    }


    public RowSet findListType() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        CachedRowSetImpl crsi = null;
        String sql = "select typeId ,typeName from bc_types limit 0,7;";
        conn = this.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            crsi = new CachedRowSetImpl();
            crsi.populate(rs);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeSource(rs, null, ps, conn);
        }

        return crsi;
    }


    public RowSet findListChildType() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        CachedRowSetImpl crsi = null;
        String sql = "select bc_childtypes.typeId ,childTypeId, childTypeName  " +
                "from bc_childtypes left join bc_types  on bc_types.typeId = bc_childtypes.typeId";

        conn = this.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            crsi = new CachedRowSetImpl();
            crsi.populate(rs);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeSource(rs, null, ps, conn);
        }

        return crsi;
    }


    public RowSet findListChildTypeByTypeId(int typeId) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        CachedRowSetImpl crsi = null;
        String sql = "select topicId, topicName from qa_topics where typeId=?";

        conn = this.getConnection();
        try {
            ps = conn.prepareStatement(sql);
            ps.setInt(1, typeId);
            rs = ps.executeQuery();
            crsi = new CachedRowSetImpl();
            crsi.populate(rs);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            this.closeSource(rs, null, ps, conn);
        }

        return crsi;
    }
}
